package com.jb.repair.acticity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jb.repair.R;
import com.jb.repair.common.AppConfig;
import com.jb.repair.dao.Db;
import com.jb.repair.model.SbbdzdyModel;
import com.jb.repair.model.YcjlModel;
import com.jb.repair.util.DownAttachmentDialogUtil;
import com.jb.repair.util.DownFileAsyncTask;
import com.jb.repair.util.IntentUtils;
import com.jb.repair.util.StringUtils;
import com.jb.repair.view.InfoTextView;
import com.jb.repair.view.TitleView;

import java.io.File;
import java.util.List;

/**
 * 异常记录详情页
 */
public class ExceptionRecordActivtity extends BaseActivity {

    private TitleView titleView;
    private InfoTextView tv_begin_time, tv_deal, tv_people,
            tv_deal_time, tv_exception_info, tv_report,
            tv_source_id, tv_foundman_id, tv_abn_status, tv_note;
    private LinearLayout ll_ycjl;
    private Db db;
    private int i = -1;

    private String exceptionInfo = "";
    private static final String TAG = "ExceptionRecordActivtity";
    private YcjlModel model;
    private DownAttachmentDialogUtil dialogUtil;


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ycjlxq);
        initviews();
        db = Db.getInstance(this);
        model = (YcjlModel) getIntent().getSerializableExtra("ycjl");
        String form_CODE = db.getName2("CODE", "TB_SYS_CODE", "CODE_NAME", "异常记录");
        Log.e("1231", form_CODE);
        String id_zhuanye = getIntent().getStringExtra("id_zhuanye");

//        int i = model.abn_time.lastIndexOf(".");
//        if (-1 != i){
//            tv_begin_time.setText(model.abn_time.substring(0,i));
//        }else{
        tv_begin_time.setText(model.found_time);
//        }

        db = Db.getInstance(this);
//        Log.e("NNN",model.dealtype);
        String name = db.getName("CODE_NAME", "TB_SYS_CODE", "CODE", model.dealtype_id);
        tv_deal.setText(name);
        String visible = db.getVisible(form_CODE, "solvedman_id_editable");
        if (!visible.equals("T")) {
            tv_people.setVisibility(View.GONE);
        }
        String name3 = db.getName("PERS_NAME", "TB_SYS_PERSON", "GUID", model.solvedman_id_editable);

        tv_people.setText(name3);
//        Log.e("MMM",model.process_time+3);
//        if(StringUtils.isNullOrEmpty(model.process_time)){
//            tv_deal_time.setText(model.process_time);
//        }else {
//            int j = model.process_time.lastIndexOf(".");
//            if (-1 != j) {
//                tv_deal_time.setText(model.process_time.substring(0, j));
//            } else {
        String visible1 = db.getVisible(form_CODE, "solved_time_editable");
        if (!visible1.equals("T")) {
            tv_deal_time.setVisibility(View.GONE);
        }
        tv_deal_time.setText(model.solved_time_editable);
        exceptionInfo = model.abn_describe;
        tv_exception_info.setText(exceptionInfo);
        Log.e("tv_attachment", model.attachment);

        String name2 = db.getName("WJMC", "TB_FILE_LB", "GUID", model.attachment);
        if (!StringUtils.isNullOrEmpty(name2)) {
            tv_report.setText(name2);
            i = 1;
        } else {
            String name5 = db.getName("WJMC", "TB_FILE_LB", "OBJ_ID", model.attachment);
            tv_report.setText(name5);
        }
        String source_id = db.getName("CODE_NAME", "TB_SYS_CODE", "CODE", model.source_id);
        tv_source_id.setText(source_id);
        String foundman_id = db.getName("PERS_NAME", "TB_SYS_PERSON", "GUID", model.foundman_id);
        tv_foundman_id.setText(foundman_id);
        tv_abn_status.setText(model.abn_status);
        String inspect_id = db.getName("CODE_NAME", "TB_SYS_CODE", "CODE", model.inspect_id);
        String patrol_id = db.getName("CODE_NAME", "TB_SYS_CODE", "CODE", model.patrol_id);
        tv_note.setText(model.note);

        //----------------------------------------------处理自定义字段逻辑-----------------------------------------------------------
        //查找自定义字段显示
        //拿到表单编码
        List<SbbdzdyModel> sbbdzdyModelList = db.getSbbdzdyModel(form_CODE, id_zhuanye);
        for (SbbdzdyModel model1 : sbbdzdyModelList) {
            if (model1.is_show_eqbook.equals("T")) {
                InfoTextView infoTextView = new InfoTextView(this);
                ll_ycjl.addView(infoTextView);
                //需要设置key的值
                infoTextView.setKey(model1.cus_name);
                //设置value的值
                String format = model1.field_format;
                String value = db.getZdyzdValue(model1.guid, model.guid);
                if (value == null) {
                    value = "";
                } else if (format != null && "时间".equals(format) && value.length() > 10) {
                    value = value.substring(0, 10);
                }
                infoTextView.setText(value);


//                String zdyzdValue = db.getZdyzdValue(model1.guid, model.guid);
//                infoTextView.setText(zdyzdValue);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = (int) getResources().getDimension(R.dimen.ledger_info_textsize);
                infoTextView.setLayoutParams(params);
            }
        }
        fillFakeData();
//-------------------------------------------------------------------------------------------------------

        //tv_begin_time.setText(model.found_time);
        //db = Db.getInstance(this);
        //tv_deal.setText(model.dealtype);
        //String name = F1Application.getUserName();
        //tv_people.setText(name);
        //tv_deal_time.setText(model.creat_time);
        //tv_exception_info.setText(model.abn_describe);

        //String name2 = db.getName("WJMC","TB_FILE_LB","OBJ_ID",model.attachment);
        //tv_report.setText(name2);
    }

    private void initviews() {
        titleView = (TitleView) this.findViewById(R.id.titleView);
        tv_begin_time = (InfoTextView) this.findViewById(R.id.tv_begin_time);
        tv_deal = (InfoTextView) this.findViewById(R.id.tv_deal);
        tv_people = (InfoTextView) this.findViewById(R.id.tv_people);
        tv_deal_time = (InfoTextView) this.findViewById(R.id.tv_deal_time);
        tv_exception_info = (InfoTextView) this.findViewById(R.id.tv_exception_info);
        tv_report = (InfoTextView) this.findViewById(R.id.tv_report);
        tv_source_id = (InfoTextView) this.findViewById(R.id.tv_source_id);
        tv_foundman_id = (InfoTextView) this.findViewById(R.id.tv_foundman_id);
        tv_abn_status = (InfoTextView) this.findViewById(R.id.tv_abn_status);
        tv_note = (InfoTextView) this.findViewById(R.id.tv_note);
        ll_ycjl = (LinearLayout) findViewById(R.id.ll_ycjl);


        titleView.setOnClickListener(new TitleView.OnTitleViewClickListener() {
            @Override
            public void onLeftImageClick(View view) {
                super.onLeftImageClick(view);
                finish();
            }
        });
    }

    /**
     * 填充假数据
     */
    private void fillFakeData() {

        if (exceptionInfo.equals("异响")) {
            tv_report.setText("异常报告 - 副本.docx");
        }

        tv_report.setOnClickListener(new View.OnClickListener(
        ) {
            public void onClick(View view) {

                Log.i("点击测试", "收到点击事件");
                //附件的guid
                final String guid;
                //附件的文件类型
                final String fjlx;
                if (i == 1) {
                    guid = db.getName("GUID", "TB_FILE_LB", "GUID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "GUID", model.attachment);
                } else {
                    guid = db.getName("GUID", "TB_FILE_LB", "OBJ_ID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "OBJ_ID", model.attachment);
                }
                //判断是否有附件
                boolean nullOrEmpty = StringUtils.isNullOrEmpty(tv_report.getText());
                Log.e("LLL", String.valueOf(nullOrEmpty));
                if (nullOrEmpty) {
                    return;
                } else {
                    //有附件
                    String filePath = AppConfig.ATTACHMENT_PATh + guid + "." + fjlx;
                    Log.e("1111", filePath);
                    File file = new File(filePath);
                    if (!file.exists()) {
                        Toast.makeText(ExceptionRecordActivtity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                        dialogUtil = new DownAttachmentDialogUtil(ExceptionRecordActivtity.this, R.style.myDialog,
                                new DownAttachmentDialogUtil.AttachDialogListener() {
                                    @Override
                                    public void onClick(View v) {
                                        switch (v.getId()) {
                                            case R.id.attach_dialog_canlebtn:
                                                dialogUtil.dismiss();
                                                break;
                                            case R.id.attach_dialog_surebtn:
                                                new DownFileAsyncTask(ExceptionRecordActivtity.this, guid, model.attachment, fjlx).execute();


                                                dialogUtil.dismiss();

                                                break;
                                        }
                                    }

                                });
                        Window win = dialogUtil.getWindow();
                        win.setGravity(Gravity.CENTER);
                        dialogUtil.show();
                    } else {
                        IntentUtils.JumpToIntent(ExceptionRecordActivtity.this, fjlx, filePath);

                    }

                }
            }
        });
    }
}
